<template>
	<view class="pageAll" v-if="isAlready">
		<scroll-view v-if="browseInfo" :scroll-top="scrollTop" scroll-y="true" class="scrollPage" @scroll="scroll"
		 scroll-with-animation="true">
			<view class="scrollPageTop">
				<!-- <view class="fileCoverImg">
					<fileCover :imgSrc="fileCoverImg" />
				</view> -->
				<view class="browseInfo" v-if="browseInfo.title">
					<articleInfo :info="browseInfo" @showFile="showFile" :fileType="fileType"/>
				</view>
			</view>

			<view class="content">
				<uParse :content="richText" noData="暂时没有数据"></uParse>
			</view>

		</scroll-view>
		<nonePage v-else />
		<!-- 放外面，避免滚动是跟着动 -->
		<flexBtn @click.native="goTop" />
	</view>
</template>

<script>
	import flexBtn from '../../../components/commonCom/flexBtn.vue'
	import fileCover from '../../../components/indexCom/fileCover.vue'
	import articleInfo from '../../../components/indexCom/articleInfo.vue'
	import nonePage from '../../../components/commonCom/nonePage.vue'
	import uParse from '@/components/gaoyia-parse/parse.vue'
	export default {
		data() {
			return {
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				browseInfo: null,
				// fileCoverImg: require('../../../static/index/sopFile.png'),
				richText: '',
				fileType: '',
				isAlready: false,
			}
		},
		methods: {
			// 查看文件
			showFile(id) {
				if (this.fileType === 1) {
					if(this.browseInfo.file) {
						uni.showLoading({
							title:'文件下载中',
							mask:true,
						})
						
						uni.downloadFile({
							url: this.browseInfo.file,
							success: function(response) {
								var filePath = response.tempFilePath;
								uni.openDocument({
									filePath: filePath,
									success: function(data) {
										uni.hideLoading()
									},
									fail() {
										uni.hideLoading()
									}
								});
							}
						});
					} else {
						uni.showToast({
							title:'文件内容为空',
							icon:"none",
						})
					}
					
				}

			},
			// 监听滚动事件
			scroll(e) {
				this.old.scrollTop = e.detail.scrollTop
			},
			// 滚动条移动到顶部
			goTop() {
				this.scrollTop = this.old.scrollTop
				this.$nextTick(function() {
					this.scrollTop = 0;
				});
			},
			// 获取文件或者活动的数据
			getFileInfo(type, id) {
				if (type === 1) {
					this.$api.noticeMessage({
							id: id
						})
						.then(res => {
							if (res.status) {
								this.isAlready = true
								this.browseInfo = res.data
								if (this.browseInfo) {
									this.richText = res.data.content
									uni.setNavigationBarTitle({
										title: res.data.title
									});
								}
								// 增加浏览数
								this.$api.addNoticeViews({
										id: id
									})
									.then(res => {})
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1500,
								})
							}
						})
				} else {
					this.$api.activityMessage({
							id: id,
						})
						.then(res => {
							if (res.status) {
								this.isAlready = true
								this.browseInfo = res.data
								if (this.browseInfo) {
									this.richText = res.data.content
									uni.setNavigationBarTitle({
										title: res.data.title
									});
								}
								// 增加浏览数
								this.$api.addActivityViews({
										id: id
									})
									.then(res => {})
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1500,
								})
							}
						})
				}
			},
		},
		components: {
			articleInfo, // 文章浏览信息
			fileCover, // 文章封面
			flexBtn, // 返回顶部按钮
			nonePage,
			uParse,
		},
		onLoad(option) {
			// 获取文件类型， 公告 1 或 活动 2
			this.fileType = parseInt(option.type)
			this.getFileInfo(parseInt(option.type), parseInt(option.id))
		},
		onShareAppMessage: (options) => {
			// 设置菜单中的转发按钮触发转发事件时的转发内容
			var shareObj = {
				title: "红标HONBIAO", // 默认是小程序的名称(可以写slogan等)
				path: '/pages/indexPage/authorization/authorization', // 默认是当前页面，必须是以‘/’开头的完整路径
				imageUrl: '/static/index/labelRouge.png', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
				success: (res) => {
					// 转发成功之后的回调
					if (res.errMsg == 'shareAppMessage:ok') {}
				},
				fail: () => {
					// 转发失败之后的回调
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						// 用户取消转发
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				}
			};
			return shareObj
		},
	}
</script>

<style scoped>
	.pageAll {
		box-sizing: border-box;
		background-color: #fdf5f3;
		width: 100%;
		height: 100%;
		display: inline-block;
		overflow-y: hidden;
		position: fixed;
	}

	.scrollPage {
		box-sizing: border-box;
		height: 100%;
	}

	.scrollPageTop {
		padding: 17upx;
		border-radius: 15upx;
	}

	.fileCoverImg {
		height: 328.66upx;
		margin: 8upx 0 17upx 0;
	}

	.browseInfo {
		height: 120upx;
		box-shadow: 0 0 15upx 5upx rgba(179, 31, 31, 0.15);
		border-radius: 15upx;
	}

	.content {
		margin: 0 17upx 17upx 17upx;
		background-color: #FFF;
		border-radius: 15upx;
		overflow: hidden;
	}
</style>
